package com.burgeon.backend.SHOP.controller;

import com.burgeon.backend.SHOP.entity.Basket;
import com.burgeon.backend.SHOP.entity.Goods;
import com.burgeon.backend.SHOP.mappers.GoodsMapper;
import com.burgeon.backend.SHOP.service.BasketService;
import com.burgeon.backend.SHOP.service.ex.OrderExceptionApp;
import com.burgeon.backend.SHOPBACKEND.rest.GetGoodsCode;
import com.burgeon.backend.entity.Info;
import com.burgeon.backend.mappers.AccountMapper;
import com.burgeon.backend.stsutil.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("shop/basket")
public class BasketController extends BaseController {
    @Autowired
    private BasketService basketService;
    @Autowired
    private AccountMapper accountMapper;
    @Autowired
    private GoodsMapper goodsMapper;

    @RequestMapping("count")
    public ResponseResult<String> basketCount(HttpSession session){
        if (null == session.getAttribute("uid")){
            return new ResponseResult<String>(SUCCESS,"0");
        }
        String uid = session.getAttribute("uid").toString();
        String token = session.getAttribute("token").toString();
        String data = basketService.getBasketCount(uid, token);
        return new ResponseResult<String>(SUCCESS,data);
    }

    @RequestMapping("paygoodsdata")
    public ResponseResult<List<Goods>> GetPayGoodsData(HttpSession session){
        String uid = session.getAttribute("uid").toString();
        String token = session.getAttribute("token").toString();
        String discount;
        if (null != session.getAttribute("user_discount")) {
            discount = session.getAttribute("user_discount").toString();
        }else {
            discount = "1";
        }
        String isPlus;  //是plus session会赋值Y  不是为null
        if (null!=session.getAttribute("user_is_plus")){
            isPlus = session.getAttribute("user_is_plus").toString();
        }else {
            isPlus = "N";
        }


        List<Goods> data = basketService.GetPayGoodsData(uid,token,discount,isPlus);
        return new ResponseResult<List<Goods>>(SUCCESS,data);
    }

    @RequestMapping("del")
    public ResponseResult<Void> delData(String id){
        basketService.DelBasket(id);
        return new ResponseResult<>(SUCCESS);
    }

    @RequestMapping("set")
    public ResponseResult<Void> setData(String id,String num,String selected,HttpSession session){
        String token = session.getAttribute("token").toString();
        basketService.SetBasketData(id,num,selected,token);
        return new ResponseResult<>(SUCCESS);
    }

    @RequestMapping("data")
    public ResponseResult<List<Basket>> getData(HttpSession session) {
        if (session.getAttribute("uid")==null){
            return new ResponseResult<>(NOTLOG);
        }
        String discount;
        if (null != session.getAttribute("user_discount")) {
            discount = session.getAttribute("user_discount").toString();
        }else {
            discount = "1";
        }
        String isPlus;  //是plus session会赋值Y  不是为null
        if (null!=session.getAttribute("user_is_plus")){
            isPlus = session.getAttribute("user_is_plus").toString();
        }else {
            isPlus = "N";
        }
        String uid = session.getAttribute("uid").toString();
        String token = session.getAttribute("token").toString();

        List<Basket> data = basketService.GetBasket(uid,token,discount,isPlus);

        return new ResponseResult<List<Basket>>(SUCCESS,data);
    }

    @RequestMapping("add")
    public ResponseResult<Void> add(String gid, String code, String color, String size, HttpSession session) {

        if (null == session.getAttribute("uid")) {
            return new ResponseResult<>(NOTLOG);
        }

        String token = session.getAttribute("token").toString();
        String uid = session.getAttribute("uid").toString();

        String barcode = goodsMapper.getSizeCodeByCodeColorSize(code,color,size);

        if (null == barcode){
            Info info = accountMapper.GetTokenData(token);
            GetGoodsCode getGoodsCode = new GetGoodsCode(info.getUrl(), info.getAccount(), info.getPassword());
            try {
                code = getGoodsCode.getData(code, color, size);
            } catch (Exception e) {
                e.printStackTrace();
                throw new OrderExceptionApp("出现错误");
            }
        }else {
            code = barcode;
        }

        basketService.addBasket(uid, gid,color+"-"+size, code, token);
        return new ResponseResult<>(SUCCESS);
    }


}
